package com.hoolai.thrift;

import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;

import com.hoolai.gen.thrift.HelloWorldService;


public class HelloServerDemo {
	public static final int SERVER_PORT = 8083;

	public void startServer() {
		try {
			System.out.println("HelloWorld TSimpleServer start ...." + SERVER_PORT);

			// TProcessor tprocessor = new
			// HelloWorldService.Processor<HelloWorldService.Iface>(new
			// HelloWorldImpl());
			HelloWorldService.Processor<HelloWorldService.Iface> tprocessor = new HelloWorldService.Processor<HelloWorldService.Iface>(
					new HelloWorldImpl());

			// 简单的单线程服务模型，一般用于测试
			TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
			TServer.Args tArgs = new TServer.Args(serverTransport);
			tArgs.processor(tprocessor);
			// tArgs.protocolFactory(new TBinaryProtocol.Factory());
			tArgs.protocolFactory(new TCompactProtocol.Factory());
			// tArgs.protocolFactory(new TJSONProtocol.Factory());
			TServer server = new TSimpleServer(tArgs);
			server.serve();
		} catch (Exception e) {
			System.out.println("Server start error!!!");
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HelloServerDemo server = new HelloServerDemo();
		server.startServer();
	}

}
